<?php
require('config.php');
	function getDistance($lat1, $lng1, $lat2, $lng2, $len_type = 1, $decimal = 2)
	{ 
	 	$EARTH_RADIUS=6378.137; 
	 	$PI=3.1415926; 
	 	$radLat1 = $lat1 * $PI / 180.0;
	 	$radLat2 = $lat2 * $PI / 180.0;
	 	$a = $radLat1 - $radLat2; 
	 	$b = ($lng1 * $PI / 180.0) - ($lng2 * $PI / 180.0);
	 	$s = 2 * asin(sqrt(pow(sin($a/2),2) + cos($radLat1) * cos($radLat2) * pow(sin($b/2),2)));
	 	$s = $s * $EARTH_RADIUS; 
	 	$s = round($s * 1000); 
	 	if ($len_type > 1) 
	 	{ 
	 		$s /= 1000; 
	 	} 
	 	return round($s,$decimal); 
	}
		$catName=$_REQUEST['catName'];
		$shaixuan=$_REQUEST['shaixuan'];
		$type=$_REQUEST['type'];
		$lat=$_REQUEST['lat'];
		$lon=$_REQUEST['lon'];
		$user_id='0';
		if($_REQUEST['user_id']) $user_id=$_REQUEST['user_id'];
		$sql="select * from `".$oto."_help_cate` where `cateName`='{$catName}'";
		$result=$db->query($sql);
		while($row=$result->fetch_assoc()){
			$help_cate[]=$row;//将取得的所有数据赋值给person_info数组
		}
		$cid=0;
		if(isset($help_cate))
		{
			$cid=$help_cate[0]['id'];
		}
		$sql="select ".$oto."_help.*,".$oto."_users.userName,".$oto."_users.userPhoto,".$oto."_users.grage,".$oto."_users.userBond,".$oto."_users.isBondShow,".$oto."_users.userPhone from `".$oto."_help`,".$oto."_users where cid='{$cid}' and `type`='{$type}' and isShow='1' and isDel='0' and ".$oto."_users.userId=".$oto."_help.userId ";
		if($type==-1 || $type=='-1')
		$sql="select ".$oto."_help.*,".$oto."_users.userName,".$oto."_users.userPhoto,".$oto."_users.grage,".$oto."_users.userBond,".$oto."_users.isBondShow,".$oto."_users.userPhone from `".$oto."_help`,".$oto."_users where cid='{$cid}' and isShow='1' and isDel='0' and ".$oto."_users.userId=".$oto."_help.userId ";
		if($catName=="所有发布")
		{
			$sql="select ".$oto."_help.*,".$oto."_users.userName,".$oto."_users.userPhoto,".$oto."_users.grage,".$oto."_users.userBond,".$oto."_users.isBondShow,".$oto."_users.userPhone from `".$oto."_help`,".$oto."_users where isShow='1' and `type`='{$type}' and isDel='0' and ".$oto."_users.userId=".$oto."_help.userId ";
			if($type==-1 || $type=='-1')
			$sql="select ".$oto."_help.*,".$oto."_users.userName,".$oto."_users.userPhoto,".$oto."_users.grage,".$oto."_users.userBond,".$oto."_users.isBondShow,".$oto."_users.userPhone from `".$oto."_help`,".$oto."_users where isShow='1' and isDel='0' and ".$oto."_users.userId=".$oto."_help.userId ";
		}
		if($shaixuan=="时间")
		{
			$sql.=" order by createTime DESC";
		}
		else if($shaixuan=="点击量")
		{
			$sql.=" order by count DESC";
		}
		else if($shaixuan=="等级")
		{
			$sql.=" order by grage DESC";
		}
		$result=$db->query($sql);
		while($row=$result->fetch_assoc()){
			$help[]=$row;//将取得的所有数据赋值给person_info数组
		}
		if(isset($help))
		{
			for($i=0;$i<count($help);$i++)
			{
				$lat2=$help[$i]['lat'];
				$lon2=$help[$i]['lon'];
				$wb_id=$help[$i]['id'];
				$sql="select * from `".$oto."_help_order_user` where `help_id`='{$wb_id}' and isDel='0'";
				$result=$db->query($sql);
				while($row=$result->fetch_assoc()){
					$order_user[$i][]=$row;//将取得的所有数据赋值给person_info数组
				}
				if(isset($order_user[$i]))
				{
					$help[$i]['jiedan_num']=count($order_user[$i]);
				}
				else $help[$i]['jiedan_num']=0;
				$help[$i]['img_list']=explode("|",$help[$i]['img']);
				$help[$i]['juli']=getDistance($lat,$lon,$lat2,$lon2);
				$fanwei=$help[$i]['range']*1000;
				$help[$i]['fanwei_pan']=1;
				if($help[$i]['juli']>$fanwei)
				{
					$help[$i]['fanwei_pan']=0;
					// array_splice($help, $i, 1);
					// $i--;
				}
				if($help[$i]['range']==0 || $help[$i]['range']=='0') $help[$i]['fanwei_pan']=1;

					$sql="select * from ".$oto."_help_order_user where help_id='{$wb_id}' and user_id='{$user_id}' and isDel='0'";
					$result=$db->query($sql);
					while($row=$result->fetch_assoc()){
						$isOrderUser[$i][]=$row;//将取得的所有数据赋值给person_info数组
					}
					if(isset($isOrderUser[$i]))
					{
						$help[$i]['isGo']=-1;
					}
					else $help[$i]['isGo']=1;
				$sql="select * from ".$oto."_help_orders where helpId='{$wb_id}' and isDel='0' and orderStatus not in ('-1','-2','-5','-6')";
				$result=$db->query($sql);
				while($row=$result->fetch_assoc()){
					$isOrder[$i][]=$row;//将取得的所有数据赋值给person_info数组
				}
				if(isset($isOrder[$i]))
				{
					if($help[$i]['isGo']==1) $help[$i]['isGo']=0;
				}
			}
			if($shaixuan=="距离")
			{
				for($i=0;$i<count($help);$i++)
				{
					for($j=$i;$j<count($help);$j++)
					{
						if($help[$j]['juli']<$help[$i]['juli'])
						{
							$t=$help[$i];
							$help[$i]=$help[$j];
							$help[$j]=$t;
						}
					}
				}
			}
			echo json_encode($help);
		}
		else echo 'null';
?>